﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTelluro.Explorer.Infrastructure.CrossCutting.SSO;
using iTelluro.SYS.Entity;
using iTelluro.SSO.WebServices;
using iTelluro.SSO.WebServices.Admin;
using iTelluro.Explorer.Infrastructure.CrossCutting.Caching;
using iTelluro.Explorer.Infrastructure.CrossCutting.NetFramework.Caching;

namespace iTelluro.Explorer.Infrastructure.CrossCutting.NetFramework.SSO
{
    public class WebSSO : ISSO
    {
        private string _userID;

        public string UserID
        {
            get
            {
                if (!string.IsNullOrEmpty(_token) && _token.Split(',').Length > 1)
                {
                    _userID=  _token.Split(',')[1];
                }
                return _userID;
            }
        }

        private string _token;

        public string Token
        {
            get
            {
                return _token;
            }
            set
            {
                _token = value;
                ///创建UserWS时效率较低
                //if (!string.IsNullOrEmpty(_token) && _token.Split(',').Length > 1)
                //{
                //    _userID = _token.Split(',')[1];
                //}
                //_user = new UserWS(this._token, this.modID, this.modSignature);
            }
        }
        public string modID
        {
            get { return "2359bc26-2094-4ad3-9c38-0cdd81fa84bf"; }
        }

        public string modSignature
        {
            get { return "fzKyrxzODwmqweN90SqL3VynEIwIr+shX2zhK3cCvtl6ntvdmEGFOBSWZ7KiivZu8wOMqGxtE57HVcSh10BHdQooBoKoL5tlpIDvfZJazS1CxnrVOUZ6qKVPQvabFjNDtXlogkHaA860myWzgQqGkgHNCitJVTiEB985mV5D0uY="; }
        }
        private UserWS _user;
        private ICache _cacheObj;
        public ICache Cache
        {
            get
            {
                return _cacheObj;
            }
            set
            {
                _cacheObj = value;
            }
        }
        public WebSSO()
        {
        }
        string[] AuthDistrictCodes;
        string[] AuthMenuID;
        #region ISSO 成员

        private UserWS GetUserWS()
        {
            _user = new UserWS(this._token, this.modID, this.modSignature);
            return _user;
        }

        public string[] FetchAllAuthDistrictZone()
        {
            UserWS user = GetUserWS();
            object value = null;
            if (user != null)
            {
                string key = UserID + "_DistrictZone";
                if (!_cacheObj.GetItem(key, out value))
                {
                    List<DistrictDict> allDistrict = user.AllAuthZoneList;
                    List<string> result = new List<string>();
                    foreach (iTelluro.SYS.Entity.DistrictDict d in allDistrict)
                    {
                        if (d.DistrictCode.EndsWith("0000"))//省或直辖市
                        {
                            result.Add(d.DistrictCode);
                        }
                        if (!d.DistrictCode.EndsWith("0000") && d.DistrictCode.EndsWith("00"))//市
                        {
                            result.Add(d.DistrictCode);
                            result.Remove(d.DistrictCode.Substring(0, 2) + "0000");//移除省
                        }
                        if (!d.DistrictCode.EndsWith("00"))//县
                        {
                            result.Add(d.DistrictCode);
                            result.Remove(d.DistrictCode.Substring(0, 2) + "0000");//移除省
                            result.Remove(d.DistrictCode.Substring(0, 4) + "00");//移除市 
                        }
                    }
                    AuthDistrictCodes = result.ToArray();
                    _cacheObj.PutItem(key, AuthDistrictCodes, new string[0], TimeSpan.Zero, DateTime.Now.AddMinutes(1));
                }
                else
                    return value as string[];
            }
            return AuthDistrictCodes;
        }

        public string[] FetchAllAuthMenuID()
        {
            UserWS user = GetUserWS();
            object value = null;
            if (user != null)
            {
                string key = UserID + "_AuthMenu";
                if (!_cacheObj.GetItem(key, out value))
                {
                    List<string> result = new List<string>();
                    foreach (iTelluro.SYS.Entity.MenuFunc m in GetUserWS().MenuList)
                    {
                        result.Add(m.MenuCode);
                    }
                    AuthMenuID = result.ToArray();
                    _cacheObj.PutItem(key, AuthDistrictCodes, new string[0], TimeSpan.Zero, DateTime.Now.AddMinutes(1));
                }
                else
                    return value as string[];
            }
            return AuthMenuID;
        }

        #endregion

    }
}
